﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    public class SinacorMessageBoxWindow : System.Windows.Window
    {
        #region Properties
        /// <summary>
        /// Armazenará o valor do botão pressionado pelo usuário
        /// </summary>
        private SinacorMessageBoxResult _retorno;
        public SinacorMessageBoxResult Retorno
        {
            get
            {
                return _retorno;
            }
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Construtor da classe SinacorMessageBoxInterface
        /// </summary>
        /// <param name="message">Mensagem que será exibida na janela</param>
        /// <param name="title">Título da janela</param>
        /// <param name="sinacorMessageBoxButton">Conjunto de botões a serem exibidos na janela</param>
        /// <param name="sinacorMessageBoxImage">Imagem a ser exibida na janela</param>
        public SinacorMessageBoxWindow(String message, String title, SinacorMessageBoxButton sinacorMessageBoxButton, SinacorMessageBoxImage sinacorMessageBoxImage, double? windowHeight, double? windowWidth)
        {
            this.Title = title;
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            SetValue(MessageProperty, message);
            SetValue(ButtonProperty, sinacorMessageBoxButton);
            SetValue(MessageBoxIconProperty, sinacorMessageBoxImage);

            this.SetResourceReference(StyleProperty, "ControlMessageBoxWindow");
            
            if(windowHeight.HasValue)
                this.Height = windowHeight.Value;
            if(windowWidth.HasValue)
                this.Width = windowWidth.Value;

            CommandBindings.Add(new CommandBinding(SinacorWindowCommands.Close, btnClose_Click));
            CommandBindings.Add(new CommandBinding(SinacorMessageBoxCommands.Cancel, btnCancel_Click));
            CommandBindings.Add(new CommandBinding(SinacorMessageBoxCommands.No, btnNo_Click));
            CommandBindings.Add(new CommandBinding(SinacorMessageBoxCommands.OK, btnOK_Click));
            CommandBindings.Add(new CommandBinding(SinacorMessageBoxCommands.Yes, btnYes_Click));

			this.TryUpdateLayout();
        }

		void TryUpdateLayout()
		{
			try { this.UpdateLayout(); }
			catch { }
		}
        #endregion

        #region Public Methods
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            //try
            //{
                Grid header = this.GetTemplateChild("GridDragMove") as Grid;
                header.MouseLeftButtonDown += new MouseButtonEventHandler(header_MouseLeftButtonDown);
            //}
            //catch (Exception e) { }
        }

        #endregion

        #region Private Methods
        private void header_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            _retorno = SinacorMessageBoxResult.None;
            this.Close();
        }

        private void btnYes_Click(object sender, RoutedEventArgs e)
        {
            _retorno = SinacorMessageBoxResult.Yes;
            this.Close();
        }

        private void btnNo_Click(object sender, RoutedEventArgs e)
        {
            _retorno = SinacorMessageBoxResult.No;
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            _retorno = SinacorMessageBoxResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            _retorno = SinacorMessageBoxResult.Ok;
            this.Close();
        }
        #endregion

        ///<summary>
        ///Propriedade utilizada para armazenar o texto a ser exibido na MessageBox
        ///</summary>
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(string), typeof(SinacorMessageBoxWindow), new FrameworkPropertyMetadata(null));

        public string Message
        {
            get { return (string)GetValue(MessageProperty); }
        }

        /// <summary>
        /// Propriedade utilizada para armazenar o ícone que deverá ser exibido na MessageBox
        /// </summary>
        public static readonly DependencyProperty MessageBoxIconProperty =
            DependencyProperty.Register("MessageBoxIcon", typeof(SinacorMessageBoxImage), typeof(SinacorMessageBoxWindow), new FrameworkPropertyMetadata(null));

        public SinacorMessageBoxImage MessageBoxIcon
        {
            get { return (SinacorMessageBoxImage)GetValue(MessageBoxIconProperty); }
        }


        /// <summary>
        /// Propriedade utilizada para armazenar os botões que serã exibidos na MessageBox
        /// </summary>
        public static readonly DependencyProperty ButtonProperty =
            DependencyProperty.Register("Button", typeof(SinacorMessageBoxButton), typeof(SinacorMessageBoxWindow), new FrameworkPropertyMetadata(null));

        public SinacorMessageBoxButton Button
        {
            get { return (SinacorMessageBoxButton)GetValue(ButtonProperty); }
        }
    }
}
